package com.light.mytext.mycustomview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
public class MySurfaceView2 extends SurfaceView implements Callback {

    private SurfaceHolder sfh;
    private Canvas canvas;
    private Paint paint;
    private int coordinate;
    // 固定摇杆背景圆形的半径
    private int RockerCircleR, SmallRockerCircleR;
    // 摇杆的X,Y坐标以及摇杆的半径
    private float SmallRockerCircleX, SmallRockerCircleY;

    private RudderListener listener = null; // 事件回调接口

    public MySurfaceView2(Context context) {
        super(context);
    }

    public MySurfaceView2(Context context, AttributeSet as) {
        super(context, as);
        this.setKeepScreenOn(true);
        sfh = getHolder();
        sfh.addCallback(this);
        paint = new Paint();  
        paint.setColor(Color.GREEN);
        paint.setAntiAlias(true);// 抗锯齿
        setFocusable(true);
        setFocusableInTouchMode(true);
        setZOrderOnTop(true);
        sfh.setFormat(PixelFormat.TRANSPARENT);// 设置背景透明

    }

    public void surfaceCreated(SurfaceHolder holder) {

        // 获得控件最小值
        int little = this.getWidth() < this.getHeight() ? this.getWidth()
                : this.getHeight();
        // 根据屏幕大小绘制
        SmallRockerCircleX = SmallRockerCircleY = coordinate = little / 2;
        // 固定摇杆背景圆形的半径
        RockerCircleR = (int) (little * 0.35);
        // 摇杆的半径
        SmallRockerCircleR = (int) (little * 0.15);

        draw();
    }

    /***
     * 得到两点之间的弧度
     */
    public double getRad(float px1, float py1, float px2, float py2) {
        // 得到两点X的距离
        float x = px2 - px1;
        // 得到两点Y的距离
        float y = py1 - py2;
        // 算出斜边长
        float xie = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
        // 得到这个角度的余弦值（通过三角函数中的定理 ：邻边/斜边=角度余弦值）
        float cosAngle = x / xie;
        // 通过反余弦定理获取到其角度的弧度
        float rad = (float) Math.acos(cosAngle);
        // 注意：当触屏的位置Y坐标<摇杆的Y坐标我们要取反值-0~-180
        if (py2 < py1) {
            rad = -rad;
        }
        return rad;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN
                || event.getAction() == MotionEvent.ACTION_MOVE) {
            // 当触屏区域不在活动范围内
            if (Math.sqrt(Math.pow((coordinate - (int) event.getX()), 2)
                    + Math.pow((coordinate - (int) event.getY()), 2)) >= RockerCircleR) {
                // 得到摇杆与触屏点所形成的角度
                double tempRad = getRad(coordinate, coordinate, event.getX(),
                        event.getY());
                // 保证内部小圆运动的长度限制
                getXY(coordinate, coordinate, RockerCircleR, tempRad);
            } else {// 如果小球中心点小于活动区域则随着用户触屏点移动即可
                SmallRockerCircleX = (int) event.getX();
                SmallRockerCircleY = (int) event.getY();
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // 当释放按键时摇杆要恢复摇杆的位置为初始位置
            SmallRockerCircleX = coordinate;
            SmallRockerCircleY = coordinate;
        }
        draw();
        if (listener != null) {
            listener.onSteeringWheelChanged((SmallRockerCircleX - coordinate)
                    / RockerCircleR, (coordinate - SmallRockerCircleY)
                    / RockerCircleR);
        }
        //如果是上下控制的
        int direction1=0;
        if(SmallRockerCircleY-coordinate>0){//往下
            direction1=2;
        }else  if(SmallRockerCircleY-coordinate<0){//往上
            direction1=1;
        }else {
            direction1=0;
        }
        if (listener != null) {
            listener.isUp(direction1);
        }
        //左右控制的
        int direction2=0;
        if(SmallRockerCircleX-coordinate>0){//往右
            direction2=1;
        }else  if(SmallRockerCircleX-coordinate<0){//往左
            direction2=2;
        }else {
            direction2=0;
        }
        if (listener != null) {
            listener.isTurn(direction2);
        }
        return true;
    }

    /**
     *
     * @param R
     *            圆周运动的旋转点
     * @param centerX
     *            旋转点X
     * @param centerY
     *            旋转点Y
     * @param rad
     *            旋转的弧度
     */
    public void getXY(float centerX, float centerY, float R, double rad) {
        // 获取圆周运动的X坐标
        SmallRockerCircleX = (float) (R * Math.cos(rad)) + centerX;
        // 获取圆周运动的Y坐标
        SmallRockerCircleY = (float) (R * Math.sin(rad)) + centerY;
    }

    public void draw() {
        try {
            canvas = sfh.lockCanvas();
            // canvas.drawColor(Color.WHITE);
            canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 清除屏幕
            // 设置透明度
            paint.setColor(Color.CYAN);
            // 绘制摇杆背景
            canvas.drawCircle(coordinate, coordinate, RockerCircleR, paint);
            paint.setColor(Color.RED);
            // 绘制摇杆
            canvas.drawCircle(SmallRockerCircleX, SmallRockerCircleY,
                    SmallRockerCircleR, paint);
            paint.setColor(Color.YELLOW);
            // 绘制摇杆
            canvas.drawCircle(SmallRockerCircleX, SmallRockerCircleY,
                    SmallRockerCircleR-10, paint);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            try {
                if (canvas != null)
                    sfh.unlockCanvasAndPost(canvas);
            } catch (Exception e2) {

            }
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    // 设置回调接口
    public void setRudderListener(RudderListener rockerListener) {
        listener = rockerListener;
    }

    // 回调接口
    public interface RudderListener {
        void onSteeringWheelChanged(float cross, float longitudinal);
        void isUp(int direction);//上下方向修改  0中间   1上  2下
        void isTurn(int direction);//上下方向修改  0中间   1上  2下
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(
                300,
                300
        );
    }
}
